// components/filterModal/index.js
const {
  citySubjectList,
  getScore,
} = require('../../api/api')
const {
  checkLogin
} = require('../../utils/util')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    userCitys: {
      type: Object,
      value: {}
    },
    showCity: {
      type: Boolean,
      value: true
    },
    editType: {
      type: String,
      value: 'text'
    },
    color: {
      type: String,
      value: ''
    },
    fontSize: {
      type: String,
      value: ''
    },
    mode: {
      type: String,
      value: 'default'
    },
    editColor: {
      type: String,
      value: '#cccccc'
    },
    shortName: {
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    userCitys: {},
    ids: [], //选择的学科,
    chooseList: [], //
    subjectList: [], //学科列表,
    cityList: [], //城市列表
    score: '',
    ranking: '',
    show: false,
    mainSubject: null,
    isComplete: false
  },
  observers: {
    'userCitys': function (userCitys) {
      if (userCitys.code) {
        checkLogin().then((res) => {
          this.initData()
        }).catch((e) => {
          this.setData({
            isComplete: false
          })
        })
      }

    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async initData() {
      // console.log('update')
      if (wx.getStorageSync('ids') !== '') {
        this.setData({
          checkedList: wx.getStorageSync('ids') || [],
          mainSubject: wx.getStorageSync('mainSubject') || '',
          score: wx.getStorageSync('score'),
          ranking: wx.getStorageSync('ranking'),
          year: wx.getStorageSync('year'),
        })
        var year = wx.getStorageSync('year') || 2023
        var d = new Date();
        var currentYear = d.getFullYear()
        var index = year - currentYear
        var array = ['高三', '高二', '高一']
        var text = array[index]
        this.setData({
          text,
          isComplete: true
        })
      }

      if (wx.getStorageSync('chooseList') !== '') {
        var chooseList = wx.getStorageSync('chooseList')
        const chooseListIds = chooseList.map((item) => {
          return item.subjectsId
        })
        const ids = wx.getStorageSync('ids')
        var flag = ids.sort().toString() == chooseListIds.sort().toString()
        if (flag) {
          this.setData({
            chooseList: wx.getStorageSync('chooseList'),
            isComplete: true
          })
        } else {
          this.getCitySubject()

        }
        return
      }
      this.getCitySubject()
    },
    async getCitySubject() {
      const code = wx.getStorageSync('userCitys').code
      const {
        result
      } = await citySubjectList(code)
      const checkedList = wx.getStorageSync('ids')
      const list = result.filter((item) => {
        return checkedList.indexOf(item.subjectsId) > -1
      })
      list.map((item, index) => {
        if (item.subjectsId == this.data.mainSubject) {
          list.unshift(list.splice(index, 1)[0])
        }
      })
      if (list.length > 0) {
        this.setData({
          chooseList: list
        })
        wx.setStorageSync('chooseList', list)
      } else {
        this.setData({
          isComplete: false
        })
      }
    },
    openModal() {
      this.setData({
        show: true
      })
      return
      checkLogin().then((res) => {
        this.setData({
          show: true
        })
      }).catch((e) => {
        wx.showToast({
          title: '请先登录！',
          icon: 'none'
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/login/index',
          })
        }, 500);
      })

    },
    func() {

    },
    async update() {
      this.setData({
        chooseList: wx.getStorageSync('chooseList') || [],
        checkedList: wx.getStorageSync('ids') || [],
        mainSubject: wx.getStorageSync('mainSubject') || '',
        score: wx.getStorageSync('score'),
        ranking: wx.getStorageSync('ranking'),
        userCitys: wx.getStorageSync('userCitys')
      })
      const {
        chooseList,
        checkedList,
        mainSubject,
        score,
        ranking
      } = this.data
      const params = {
        chooseList,
        checkedList,
        mainSubject,
        score,
        ranking
      }
      this.triggerEvent('update', params)
    },
  }
})
